<?php
/**
 * Created by jinqiang on 2024/9/9 16:15.
 * Copyright (c) 2024, Your Company. All rights reserved.
 */


namespace App\Http\Controllers\Tool;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class AutoController extends Controller
{
    public function execute(Request $request)
    {
        $this->validate($request, [
            'type' => 'required'
        ]);
        $param = $request->all();
        $type = $param['type'];
        $log = storage_path("logs")."/auto_{$type}.log";
        switch ($type){
            //下载数加成品加工单
            case "pullProcessing":
                if($param['processing_code'] ?? ""){
                    $cmd = 'php '.base_path()."/artisan command:{$type} --processing_code=".$param['processing_code']."> {$log} 2>&1 &";
                }else{
                    return error("加工单不能为空");
                }
                break;
            //下载数加设计档案图片
            case "downloadImg":
                if($param['number'] ?? ""){
                    $cmd = 'php '.base_path()."/artisan command:{$type} --number=".$param['number']."> {$log} 2>&1 &";
                }else{
                    return error("款号不能为空");
                }
                break;
            //下载数加物料档案图片
            case "downloadMaterialImg":
                if($param['id'] ?? ""){
                    $cmd = 'php '.base_path()."/artisan command:{$type} --id=".$param['id']."> {$log} 2>&1 &";
                }else{
                    return error("请输入物料materiel_id");
                }
                break;
            //下载数加设计档案附件
            case "downloadAttach":
                if($param['type_no'] ?? ""){
                    $cmd = 'php '.base_path()."/artisan command:{$type} --typeNo=".$param['type_no']."> {$log} 2>&1 &";
                }else{
                    return error("请输入款号type_no");
                }
                break;
            //下载班牛发票数据
            case "pullBnInvoice":
                if($param['bn_type'] == 'timeout'){
                    if($param['start'] ?? ""  && ($param['end'] ?? "")){
                        $cmd = 'php '.base_path()."/artisan command:{$type} --start='".$param['start']."' --end='".$param['end']."' > {$log} 2>&1 &";
                    }else{
                        return error("开始时间、结束时间不能为空");
                    }
                }elseif ($param['bn_type'] == 'source_id'){
                    if($param['sourceId'] ?? ""){
                        $cmd = 'php '.base_path()."/artisan command:{$type} --sourceId='".$param['sourceId']."' > {$log} 2>&1 &";
                    }else{
                        return error("订单号不能为空");
                    }
                }else{
                    return error("未知类型");
                }
                break;
            //下载巨益商品信息
            case "pullJyItem":
                if($param['s_type'] == 'productCodes' && ($param['productCodes'] ?? "")){
                    $cmd = "--productCodes=".$param['productCodes'];
                }elseif($param['s_type'] == 'skuCodes' && ($param['skuCodes'] ?? "")){
                    $cmd = "--skuCodes=".$param['skuCodes'];
                }else{
                    return error('商品编码或规格编码不能为空');
                }
                $cmd = 'php '.base_path()."/artisan command:{$type} ".$cmd."> {$log} 2>&1 &";
                break;
            //同步尺寸表
            case "pullNewProducts":
                if($param['type_no'] ?? ""){
                    $cmd = 'php '.base_path()."/artisan command:{$type} --type_no=".$param['type_no']."> {$log} 2>&1 &";
                }else{
                    return error('款号不能为空');
                }
                break;
            //更新加工单核价审核
            case "pullPricingExamine":
                if($param['processing_code'] ?? ""){
                    $cmd = 'php '.base_path()."/artisan command:{$type} --processing_code=".$param['processing_code']."> {$log} 2>&1 &";
                }else{
                    return error('加工单号不能为空');
                }
                break;
            //下载核价单数据
            case "pullPricingOrder":
                if($param['number'] ?? "" && $param['p_type'] ?? ""){
                    $cmd = 'php '.base_path()."/artisan command:{$type} --number=".$param['number']." --t73=".$param['p_type']."> {$log} 2>&1 &";
                }else{
                    return error('核价编码不能为空');
                }
                break;
            //更新调拨计划单的归还状态
            case "updateAllocationStatus":
                if($param['allocation_number'] ?? ""){
                    $cmd = 'php '.base_path()."/artisan command:{$type} --allocation_number=".$param['allocation_number']."> {$log} 2>&1 &";
                }else{
                    return error('调拨计划单号不能为空');
                }
                break;
            default:
                return error("自动化程序未添加");
        }
        ppopen($cmd);
        return success("程序已启动, 请查看日志");
    }

    public function log(Request $request)
    {
        $this->validate($request, [
            'type' => 'required'
        ]);
        $param = $request->all();
        $type = $param['type'];
        $log = storage_path("logs")."/auto_{$type}.log";
        $res = str_replace("\n", "<br>", file_get_contents($log));
        $status = Cache::get("auto_{$type}") ?: 0;
        return result(['status' => $status, 'data' => $res]);
    }
}
